﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Urbania.Utilities;

namespace Urbania.World
{
    public abstract class Tile
    {
        public Vector2 PosRender; //position where tile is drawn
        public Vector2 PosIndex;
        public const int Width = 48; //width of tile
        public const int Height = 34; //height of tile
        public const int TranslateWidth = 38; //offsets tile by set amount
        protected bool willDraw;
        public Animation Animation { get; private set; } //the tile's texture

        public TileType Type { get; set; }

        public string Filename { get; set; }
        public int FrameRate { get; set; }
        public int RepeatFrameIndex { get; set; }

        public double CostMoney { get; set; }
        public double CostBureauc { get; set; }
        public double RateIncome { get; set; }
        public double RateHappiness { get; set; }
        public double RatePollution { get; set; }
        public double RateFood { get; set; }
        public double PopulationMax { get; set; }

        public Tile()
        {
        }

        public Tile(int xIndex, int yIndex, float xPos, float yPos)
        {
            this.PosIndex = new Vector2(xIndex, yIndex);
            this.PosRender = new Vector2(xPos - TranslateWidth / 2, yPos - Height / 2);
        }

        public Tile(Tile t)
        {
            this.PosRender = new Vector2(t.PosRender.X, t.PosRender.Y);
            this.PosIndex = new Vector2(t.PosIndex.X, t.PosIndex.Y);
        }

        public void InitAnimation(int frameRate, int repeatFrameIndex)
        {
            Utilities.Loader.LoadTex("Tiles\\" + Filename); // Do not worry, checks to see if it is already loaded inside.
            Animation = new Animation();
            Animation.FrameRate = frameRate;
            Animation.RepeatFrameIndex = repeatFrameIndex;
            Animation.FrameHeight = 35;
            Animation.FrameWidth = 48;
            Animation.SpriteSheet = Utilities.Loader.GetTex("Tiles\\" + Filename);
            Animation.IsRunning = true;
        }

        public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            if (willDraw)
            {
                spriteBatch.Draw(Animation, PosRender, Color.White);
            }
        }

        private long elapsedTime = 0;
        public void Update(GameTime gameTime, Rectangle viewportBounds)
        {
            if (PosRender.X >= viewportBounds.X - 50 && PosRender.X <= viewportBounds.Right + 50)
            {
                if (PosRender.Y >= viewportBounds.Y - 50 && PosRender.Y <= viewportBounds.Bottom + 50)
                {
                    willDraw = true;
                }
                else
                {
                    willDraw = false;
                }
            }
            else
            {
                willDraw = false;
            }
            Animation.Update(gameTime);
            elapsedTime += gameTime.ElapsedGameTime.Milliseconds;
            if (elapsedTime >= 5000) //5 seconds
            {
                UserStats us = UserStats.GetUserStats();
                us.Gold += this.RateIncome;
                us.Happiness += this.RateHappiness;
                us.Pollution = (double)MathHelper.Clamp((float)us.Pollution+(float)this.RatePollution,0,100);
                elapsedTime = 0;
            }

                
        }

        public virtual void Upgrade()
        {

        }

        public override string ToString()
        {
            return "Tile: " + PosRender;
        }
    }
}
